using System;
using System.Data;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

namespace Cobalt75.GreyMatter
{
    public class GMCommandList
    {
        List<string> pCommands;
        IDbCommand pCommand;
        public GMCommandList(IDbCommand command)
        {
            pCommands = new List<string>();
            pCommand = command;
        }
        bool pExecuteAsAdded;
        public bool ExecuteAsAdded
        {
            get
            {
                return pExecuteAsAdded;
            }
            set
            {
                pExecuteAsAdded = value;
            }
        }
        public void Add(string commandText)
        {
            pCommands.Add(commandText);
            if (pExecuteAsAdded)
            {
                pCommand.CommandText = commandText;
                Console.WriteLine(pCommand.CommandText);
                pCommand.ExecuteNonQuery();
            }
        }
        public void Add(string commandText,params object[] ps)
        {
            commandText = String.Format(CultureInfo.InvariantCulture, commandText, ps);
            pCommands.Add(commandText);
            if (pExecuteAsAdded)
            {
                pCommand.CommandText = commandText;
                Console.WriteLine(pCommand.CommandText);
                pCommand.ExecuteNonQuery();
            }
        }
        string pCommandSeparator;
        public string CommandSeparator
        {
            get
            {
                return pCommandSeparator;
            }
            set
            {
                pCommandSeparator = value;
            }
        }
        public void End()
        {
            if (!pExecuteAsAdded)
            {
                pCommand.CommandText = String.Join(pCommandSeparator, pCommands.ToArray());

                Console.WriteLine(pCommand.CommandText);
                pCommand.ExecuteNonQuery();
            }
        }
    }
}
